package controllers

import (
	"cashew.com/cashew_api/dtos"
	"cashew.com/cashew_api/models"
	"cashew.com/cashew_api/services"
	"cashew.com/cashew_common/constants"
)

type PageController struct {
	BaseController
}

func (c *PageController) GetMalePage() {
	req := dtos.GetMalePageParams{}
	err := dtos.Bind(&req, c.Ctx)

	if err != nil {
		c.Error("参数错误: ", err)
		c.Result(constants.ERR_PARAM, nil)
		return
	}

	res, err := services.GetMalePage(req.UserId, req.CountryCode, req.StartPage, req.CurrentPage)
	if err != nil {
		c.Error("获取首页数据失败: ", err)
		c.Result(constants.ERR_GET_MAILPAGE_FAILD, nil)
		return
	}

	c.Result(constants.SUCCESS, res)
	return

}

func (c *PageController) GetFemalePage() {
	req := dtos.GetFemalePageParams{}
	err := dtos.Bind(&req, c.Ctx)

	if err != nil {
		c.Error("参数错误: ", err)
		c.Result(constants.ERR_PARAM, nil)
		return
	}

	res, err := services.GetFemalePage(req.UserId, req.CountryCode, req.StartPage, req.CurrentPage)
	if err != nil {
		c.Error("获取首页数据失败: ", err)
		c.Result(constants.ERR_GET_FEMAILPAGE_FAILD, nil)
		return
	}

	c.Result(constants.SUCCESS, res)
	return

}

func (c *PageController) GetHomePage() {
	req := dtos.GetHomePageParams{}
	err := dtos.Bind(&req, c.Ctx)

	if err != nil {
		c.Error("参数错误: ", err)
		c.Result(constants.ERR_PARAM, nil)
		return
	}

	// 获取用户性别
	userInfo, err := models.GetUser_profileByUserId(req.UserId)
	if err != nil {
		c.Error("获取用户性别失败: ", err)
		c.Result(constants.ERR_USER_NOT_EXIST, nil)
		return
	}

	userGender := userInfo.Gender

	var res map[string]interface{}
	if userGender == constants.SEX_MAILE {
		res, err = services.GetMalePage(req.UserId, req.CountryCode, req.StartPage, req.CurrentPage)
	} else {
		res, err = services.GetFemalePage(req.UserId, req.CountryCode, req.StartPage, req.CurrentPage)
	}

	if err != nil {
		c.Error("获取首页数据失败: ", err)
		c.Result(constants.ERR_GET_HOMEPAGE_FAILD, nil)
		return
	}

	c.Result(constants.SUCCESS, res)
	return

}
